bothraces$which <- 'Both Races'
eitherrace$which <- 'One Race'
neitherrace$which <- 'No Race'

race_df <- rbind(bothraces, eitherrace, neitherrace)

race_df$which <- factor(race_df$which, levels = c('Both Races', 'One Race', 'No Race'))

xx1 <- ggplot()+
  geom_smooth(data = race_df %>% filter(days < 1), aes(days, violence_range, color = which, fill = which, linetype = which), method = 'loess', span =.9)+
  geom_smooth(data = race_df %>% filter(days > -1), aes(days, violence_range, color = which, fill = which, linetype = which), method = 'loess', span =.5)+
  theme_bw()+
  geom_vline(xintercept = 0, color = 'red', linetype=2)+
  coord_cartesian(ylim = c(0,0.4))+
  scale_x_continuous(breaks = seq(-60,340,20))+
  scale_color_manual(values=c("#cc6677", "#E69F00", "#56B4E9"))+
  scale_fill_manual(values=c("#cc6677", "#E69F00", "#56B4E9"))+
  labs(fill = "Number of Races", linetype = 'Number of Races', color = 'Number of Races')+
  xlab('Days before/after 2022 Election')+
  ylab('Support Political Violence')+
  theme(legend.position = 'none',
        axis.text = element_text(size = 8),
        axis.title = element_text(size = 9))

xx2 <- ggplot()+
  geom_smooth(data = race_df %>% filter(days < 1), aes(days, norms_range, color = which, fill = which, linetype = which), method = 'loess', span =.9)+
  geom_smooth(data = race_df %>% filter(days > -1), aes(days, norms_range, color = which, fill = which, linetype = which), method = 'loess', span =.5)+
  theme_bw()+
  geom_vline(xintercept = 0, color = 'red', linetype=2)+
  coord_cartesian(ylim = c(0.2,0.6))+
  scale_x_continuous(breaks = seq(-60,340,20))+
  scale_color_manual(values=c("#cc6677", "#E69F00", "#56B4E9"))+
  scale_fill_manual(values=c("#cc6677", "#E69F00", "#56B4E9"))+
  labs(fill = "Number of Races", linetype = 'Number of Races', color = 'Number of Races')+
  xlab('Days before/after 2022 Election')+
  ylab('Support Norm Violations')+
  theme(legend.position = 'none',
        axis.text = element_text(size = 8),
        axis.title = element_text(size = 9))

xx3 <- ggplot()+
  geom_smooth(data = race_df %>% filter(days < 1), aes(days, affpol, color = which, fill = which, linetype = which), method = 'loess', span =.9)+
  geom_smooth(data = race_df %>% filter(days > -1), aes(days, affpol, color = which, fill = which, linetype = which), method = 'loess', span =.5)+
  theme_bw()+
  geom_vline(xintercept = 0, color = 'red', linetype=2)+
  coord_cartesian(ylim = c(20,80))+
  scale_x_continuous(breaks = seq(-60,340,20))+
  scale_color_manual(values=c("#cc6677", "#E69F00", "#56B4E9"))+
  scale_fill_manual(values=c("#cc6677", "#E69F00", "#56B4E9"))+
  labs(fill = "Number of Races", linetype = 'Number of Races', color = 'Number of Races')+
  xlab('Days before/after 2022 Election')+
  ylab('Affective Polarization')+
  theme(legend.position = 'bottom',
        axis.text = element_text(size = 8),
        axis.title = element_text(size = 9),
        legend.text=element_text(size=8),
        legend.title=element_text(size=9))


final_figure_races <- suppressMessages(cowplot::plot_grid(xx1,xx2,xx3, labels = c('A', 'B', 'C'),
                                         label_size = 9,
                                         align = "v", nrow = 3, rel_widths = c(1, 1, 1), rel_heights = c(1, 1, 1.2)))

suppressMessages(print(final_figure_races))
#ggsave(final_figure_races, file = 'fig3_final.png', units = 'in', height = 7.8, width = 7.25)